package com.gxar.marketdelivery.shared.meta.exception;

import com.gxar.marketdelivery.interfaces.web.result.APIResult;
import com.gxar.marketdelivery.interfaces.web.result.IResponseStatusMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author nice
 * @version 1.0
 * @created 2023/8/1.
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义的业务异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = APIRuntimeException.class)
    @ResponseBody
    public APIResult bizExceptionHandler(HttpServletRequest req, APIRuntimeException e) {
        log.error("发生业务异常！code={}", e.getCode(), e);
        return new APIResult<>(e);
    }


    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public APIResult exceptionHandler(HttpServletRequest req, Exception e) {
        log.error("发生异常！", e);
        return new APIResult<>(IResponseStatusMsg.APIEnum.SERVER_ERROR);
    }
}
